<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>教务</a>
        <a>
          <cite>补课管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="wodekehu" class="x-body" class="clear:both;">
    <div class="layui-row"
         style="padding-left:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">

            <div class="layui-input-inline">
                <select name="campus_id" lay-verify="required" lay-search="">
                    <option value="">选择校区</option>
                    {volist name="campus" id="campu"}
                    <option value="{$campu.id}">{$campu.campus_name}</option>
                    {/volist}
                </select>
            </div>

            <div class="layui-input-inline">
                <!--
                <input type="text" name="Remedial_name" placeholder="课程名称" autocomplete="off" class="layui-input">
                -->
                <select name="course_id" lay-search="">
                    <option value="">补课课程</option>
                    {volist name="course_list" id="vo"}
                    <option value="{$vo.id}">{$vo.course_name}</option>
                    {/volist}
                </select>
            </div>

            <div class="layui-input-inline">
                <input type="text" class="layui-input" style="width: auto;" placeholder="缺课日期" name="class_date" id="start">
            </div>

            <div class="layui-input-inline">
                <button class="layui-btn" id="search">
                    <i class="layui-icon layui-icon-search"></i>筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">
                    <i class="layui-icon"></i>重置</button>

            </div>
        </div>
    </div>
    <div class="layui-input-inline" style="margin-top: 50px;">
        <button class="layui-btn  layui-btn-normal " id="open_class">
            开班补课</button>
        <button class="layui-btn  layui-btn-warm" id="follow_class">
            跟班补课</button>
        <button class="layui-btn  layui-btn-danger" id="del_class">
            <i class="layui-icon"></i> 删除</button>

    </div>
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table', 'form', 'element','laydate', 'layer'], function () {
        var table = layui.table,
            laydate = layui.laydate,
            form = layui.form,
            element = layui.element,
            layer = layui.layer,
            $ = layui.$;

        var loading = layer.load();
        form.render(null, 'form-organization');
        form.on('submit(form-organization-search)', function (data) {
            let searchLoading = layer.load();
            table.reload('table-organization', {
                url: "{:url('Remedial/lists')}"
                , where: data.field
                , done: function () {
                    layer.close(searchLoading);
                }
            });
            return false;
        });

        $('#table-add').click(function () {
            var url = "{:url('Remedial/add')}";
            location.href = url;
        });

        //日期
        laydate.render({
            elem: '#start',
            range: true
        });

        //方法级渲染
        var tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("Remedial/lists")}'
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
            if (res.code === 1) {
                res.code = 0;
            } else {
                res.code = -1;
            }
            return {
                "code": res.code, //解析接口状态
                "msg": res.message, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.data //解析数据列表
            };
            },
            cols: [[
                {type:'checkbox'},
                {field: 'student_name', title: '姓名',width:80 ,align: 'center'},
                {field: 'student_number', title: '学号', width: 180,align: 'center'},
                {field: 'campus_name', title: '校区', width: 250,align: 'center'},
                {field: 'student_phone', title: '手机号', width: 120,align: 'center'},
                {field: 'course_name', title: '课程', width: 280,align: 'center'},
                {field: 'grades_name', title: '班级', width: 300,align: 'center'},
                {field: 'class_type', title: '上课状态', width: 120,align: 'center'},
                {field: 'content_area', title: '上课进度', width: 120,align: 'center'},
                {field: 'weekday', title: '星期', width: 100,align: 'center'},
                {field: 'class_date', title: '缺课日期', width: 250,align: 'center'},
                {field: 'absence_time', title: '缺勤时间', width: 200,align: 'center'},
                {field: 'duration', title: '时长(分钟)', width: 100,align: 'center'},
                {field: 'charging_status', title: '计费', width: 80,align: 'center'},
                {field: 'attendance_reasons', title: '缺勤原因', width: 100,align: 'center'},
                {field: 'remarks', title: '备注', width: 200,align: 'center'}
            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                layer.close(loading);
             }
        });



        //搜索项
        $('#search').click(function () {
            var field = {};
            field.course_id = $('select[name=course_id]').val();
            field.campus_id = $('select[name=campus_id] option:selected').val();

            var create_date_rang = $('input[name=class_date]').val();
            if(create_date_rang.length > 0){
                strs_date=create_date_rang.split("-"); //字符分割
                field.start_date = strs_date[0]+'-'+strs_date[1]+'-'+strs_date[2];
                field.end_date = strs_date[3]+'-'+strs_date[4]+'-'+strs_date[5];
            }

            console.log(field);
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //清除
        $('#clear').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns.reload({
                where: {
                    'course_id': '',
                    'campus_id': '',
                    'class_date': '',
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });


        //开班补课
        $('#open_class').click(function () {
            var checkStatus = table.checkStatus('datalist');
            var data = checkStatus.data;
            if(data.length <= 0){
                layer.msg('请选择需要补课的学生');
                return;
            }
            var course_id = data[0].course_id;
            var campus_id = data[0].campus_id;
            var ids_str = "";
            var student_id= [];
            for(var i =0; i < data.length; i ++){
                if(course_id != data[i].course_id){
                    layer.msg('学员的缺勤课程不相同，不能同时安排补课');
                    return;
                }

                if(campus_id != data[i].campus_id){
                    layer.msg('学员的归属校区相同，不能同时安排补课');
                    return;
                }

                if(student_id.indexOf(data[i].student_id) > -1){ //包含
                    layer.msg('勾选学员中【'+ data[i].student_name +'】有重复，不能同时安排补课');
                    return;
                }else{
                    student_id.push(data[i].student_id);
                }
                ids_str=ids_str+data[i].id+",";
                course_id = data[i].course_id;
                campus_id = data[i].campus_id;
            }
            var ids = ids_str.substr(0,ids_str.length-1);

            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '新开班补课【'+data[0].course_name+'】',
                area: ['95%', '95%'],
                shadeClose: false,
                content: "{:url('Remedial/lesson_class')}?ids="+ids +"&course_id="+course_id + "&campus_id="+campus_id,
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    body.find("input[name='coures_name']").val(data[0].course_name);

                },
                end:function () {

                    tableIns.reload();
                }

            });

        });



        //跟班补课
        $('#follow_class').click(function () {
            var checkStatus = table.checkStatus('datalist');
            var data = checkStatus.data;
            if(data.length <= 0){
                layer.msg('请选择需要补课的学生');
                return;
            }
            var course_id = data[0].course_id;
            var campus_id = data[0].campus_id;
            var ids_str = "";
            var class_subject_id_str = "";
            var student_id= [];
            for(var i =0; i < data.length; i ++){
                if(course_id != data[i].course_id){
                    layer.msg('学员的缺勤课程不相同，不能同时安排补课');
                    return;
                }
                if(student_id.indexOf(data[i].student_id) > -1){ //包含
                    layer.msg('勾选学员中【'+ data[i].student_name +'】有重复，不能同时安排补课');
                    return;
                }else{
                    student_id.push(data[i].student_id);
                }
                ids_str=ids_str+data[i].id+",";
                class_subject_id_str = class_subject_id_str + data[i].class_subject_id +",";
                course_id = data[i].course_id;

            }
            var ids = ids_str.substr(0,ids_str.length-1);
            var class_subject_id = class_subject_id_str.substr(0,class_subject_id_str.length-1);
            layer.open({
                type: 2,
                closeBtn: 2,
                title: '跟班补课【'+data[0].course_name+'】',
                area: ['90%', '95%'],
                shadeClose: false,
                content: "{:url('Remedial/follow_class')}?ids="+ids +"&course_id="+course_id + "&class_subject_id="+class_subject_id + "&campus_id="+campus_id,
                success: function (layero, index) {
                    /*
                    var body = layer.getChildFrame('body', index);
                    body.find("input[name='coures_name']").val(data[0].course_name);
                    */

                },
                end:function () {

                    tableIns.reload();
                }

            });

        });


        //删除补课【限删除未缴费项目】
        $('#del_class').click(function () {
            var checkStatus = table.checkStatus('datalist');
            var data = checkStatus.data;
            if(data.length <= 0){
                layer.msg('请选择需要删除的学生');
                return;
            }
            var ids_str = "";
            var student_name = "";
            var student_id= [];
            for(var i =0; i < data.length; i ++){
                /*
                if(data[i].charging == 1){
                    layer.msg('勾选学员中存在补课已计费记录，不能删除该补课记录');
                    return;
                }
                */

                ids_str=ids_str+data[i].id+",";
                if(student_id.indexOf(data[i].student_id) > -1) { //包含
                    continue;
                }else{
                    student_id.push(data[i].student_id);
                }
                student_name= student_name + "【"+data[i].student_name+"】";
            }
            var ids = ids_str.substr(0,ids_str.length-1);
            layer.confirm('确定删除'+student_name+'的补课记录吗?', function (index) {
                //插入学生至班级
                var field = {};
                field.ids = ids;
                $.ajax({
                    url: '/admin/remedial/del_class',
                    method: "post",
                    data: field,
                    beforeSend: function () {
                        isLoad = layer.load(0, {
                            shade: false
                        }); //0代表加载的风格，支持0-2
                    },
                    success: function (result) {
                        layer.msg(result.msg);
                        if (result.code === 1) {
                            tableIns.reload();
                        }

                    },
                    complete: function () {
                        layer.close(isLoad)
                    },
                    error: function (error) {
                        console.log(error)
                    }
                });
                return false;
            });


        });


    });
</script>
</body>
